﻿using Axis.BusinessObject.Core;
using Axis.BusinessObject.Research;
using Axis.Dao.Research;
using PhuLeBooze.Global;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;

namespace PhuLeBooze.Views.Gift.Service
{
    /// <summary>
    /// Summary description for GiftService
    /// </summary>
    public class GiftService : IHttpHandler, IReadOnlySessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["Action"];
            string output = string.Empty;
            switch (action)
            {
                case "INSERT":
                case "UPDATE":
                case "DELETE":
                    output = UpdateGift(context);
                    break;
                case "GET":
                default:
                    output = GetGift(context);
                    break;
            }
            context.Response.Write(output);
        }
        protected string UpdateGift(HttpContext context)
        {
            CCore cCore = new CCore();
            string inputValue = cCore.GetInputValue(context);
            /* Action */
            CResearchDao core = new CResearchDao();
            CApplicationMessage message = core.ActionGift(inputValue);
            string output = new JavaScriptSerializer().Serialize(message);
            return output;
        }
        protected string GetGift(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            string _search = request["_search"];
            string numberOfRows = request["rows"];
            string pageIndex = request["page"];
            string sortColumnName = request["sidx"];
            string sortOrderBy = request["sord"];
            /* Get QueryString to xml */
            CCore cCore = new CCore();
            string inputValue = cCore.GetInputValue(context);
            CResearchDao cResearchDao = new CResearchDao();
            IList<CGift> list = cResearchDao.GetGift(inputValue);
            int totalRecords = cResearchDao.LastOutputValue.TotalRows;
            int totalPages = cResearchDao.LastOutputValue.TotalPages;
            return BuildJQGridResults(list, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), totalPages);
        }
        private string BuildJQGridResults(IList<CGift> list, int numberOfRows, int pageIndex, int totalPages)
        {
            if (list != null && list.Count > 0)
            {
                CGrid.JQGridResults result = new CGrid.JQGridResults();
                List<CGrid.JQGridRow> rows = new List<CGrid.JQGridRow>();
                foreach (CGift item in list)
                {
                    CGrid.JQGridRow row = new CGrid.JQGridRow();
                    row.id = item.ID;
                    row.cell = new string[12];
                    row.cell[0] = item.ID.ToString();
                    row.cell[1] = item.Code;
                    row.cell[2] = item.Name;
                    row.cell[3] = string.Format("<img class='imgGiftImage' src='{0}' FileName='{0}'/>", item.Image);
                    row.cell[4] = item.Description;
                    row.cell[5] = item.Mark.ToString();
                    row.cell[6] = item.Amount.ToString();
                    row.cell[7] = item.Manufactor;
                    row.cell[8] = item.Quantity.ToString();
                    row.cell[9] = item.GroupName;
                    row.cell[10] = item.GroupID.ToString();
                    row.cell[11] = string.Format("<img class='imgAction' src='../../Images/16x16/edit.png' onclick=\"ShowEdit('{0}')\"/>", item.ID);
                    rows.Add(row);
                }
                result.rows = rows.ToArray();
                result.page = pageIndex;
                result.total = totalPages;
                result.records = numberOfRows;
                return new JavaScriptSerializer().Serialize(result);
            }
            else
            {
                return string.Empty;
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}